package edu.uwm.RobberKnight.View;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import edu.uwm.RobberKnight.Controller.RobberKnightsController;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

/*
 * Used to set up and add players to a game of Robber Knights.
 */
public class SetUpView extends JPanel {

    private static final long serialVersionUID = -61398493154051371L;
    private JTextField txtPlayer1Name;
    private JTextField txtPlayer2Name;
    private JTextField txtPlayer3Name;
    private JTextField txtPlayer4Name;
    private JTextField txtPlayer1Age;
    private JTextField txtPlayer2Age;
    private JTextField txtPlayer3Age;
    private JTextField txtPlayer4Age;
    private RobberKnightsController _controller;
    private boolean _finished;
    private JButton btnAddPlayer3;
    private JButton btnAddPlayer4;
    private JLabel lblPlayer3;
    private JLabel lblPlayer4;
    private JButton btnRemovePlayer3;
    private JButton btnRemovePlayer4;

    public SetUpView(RobberKnightsController controller) {
        _controller = controller;
        setLayout(null);

        final JLabel lblError = new JLabel("");
        lblError.setBounds(220, 14, 330, 14);
        lblError.setVisible(false);
        add(lblError);

        JButton btnStart = new JButton("Start");
        btnStart.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent event) {

                _finished = false;
                if (!_finished) {
                    lblError.setVisible(false);
                    LinkedList<Integer> lstErrors = new LinkedList<Integer>();
                    if (!addPlayer(txtPlayer1Name, txtPlayer1Age)) {
                        lstErrors.add(1);
                        txtPlayer1Name.setBackground(new Color(255, 65, 65));
                        txtPlayer1Age.setBackground(new Color(255, 65, 65));
                    } else {
                        txtPlayer1Name.setBackground(new Color(240, 240, 240));
                        txtPlayer1Age.setBackground(new Color(240, 240, 240));
                        txtPlayer1Name.setEditable(false);
                        txtPlayer1Age.setEditable(false);
                    }
                    if (!addPlayer(txtPlayer2Name, txtPlayer2Age)) {
                        lstErrors.add(2);
                        txtPlayer2Name.setBackground(new Color(255, 65, 65));
                        txtPlayer2Age.setBackground(new Color(255, 65, 65));
                    } else {
                        txtPlayer2Name.setBackground(new Color(240, 240, 240));
                        txtPlayer2Age.setBackground(new Color(240, 240, 240));
                        txtPlayer2Name.setEditable(false);
                        txtPlayer2Age.setEditable(false);
                    }
                    if (txtPlayer3Name.isVisible()) {
                        if (!addPlayer(txtPlayer3Name, txtPlayer3Age)) {
                            lstErrors.add(3);
                            txtPlayer3Name.setBackground(new Color(255, 65, 65));
                            txtPlayer3Age.setBackground(new Color(255, 65, 65));
                        } else {
                            txtPlayer3Name.setBackground(new Color(240, 240, 240));
                            txtPlayer3Age.setBackground(new Color(240, 240, 240));
                            txtPlayer3Name.setEditable(false);
                            txtPlayer3Age.setEditable(false);
                            btnRemovePlayer3.setEnabled(false);
                        }
                    }
                    if (txtPlayer4Name.isVisible()) {
                        if (!addPlayer(txtPlayer4Name, txtPlayer4Age)) {
                            lstErrors.add(4);
                            txtPlayer4Name.setBackground(new Color(255, 65, 65));
                            txtPlayer4Age.setBackground(new Color(255, 65, 65));
                        } else {
                            txtPlayer4Name.setBackground(new Color(240, 240, 240));
                            txtPlayer4Age.setBackground(new Color(240, 240, 240));
                            txtPlayer4Name.setEditable(false);
                            txtPlayer4Age.setEditable(false);
                            btnRemovePlayer4.setEnabled(false);
                        }
                    }
                    if (lstErrors.size() == 0) {
                        _finished = true;
                        _controller.startGame();
                    } else {
                        if (lstErrors.size() == 1) {
                            lblError.setText("Please check the errors for player" + lstErrors.get(0) + ".");
                        } else {
                        	String errorMessage = "Please check the errors for";
                        	for (int i = 0; i < lstErrors.size() - 1; i++) {
                        		errorMessage += " player" + lstErrors.get(i) + ",";
							}
                        	errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
                        	errorMessage += " and player" + lstErrors.getLast();
                            lblError.setText(errorMessage + ".");
                        }
                        lblError.setVisible(true);
                    }
                }
            }
        });
        btnStart.setBounds(292, 113, 57, 23);
        add(btnStart);

        JLabel lblPlayer1 = new JLabel("Player 1:");
        lblPlayer1.setBounds(10, 42, 46, 14);
        add(lblPlayer1);

        JLabel lblPlayer2 = new JLabel("Player 2:");
        lblPlayer2.setBounds(10, 67, 46, 14);
        add(lblPlayer2);

        lblPlayer3 = new JLabel("Player 3:");
        lblPlayer3.setVisible(false);
        lblPlayer3.setBounds(10, 92, 46, 14);
        add(lblPlayer3);

        lblPlayer4 = new JLabel("Player 4:");
        lblPlayer4.setVisible(false);
        lblPlayer4.setBounds(10, 117, 46, 14);
        add(lblPlayer4);

        txtPlayer1Name = new JTextField();
        txtPlayer1Name.setBounds(66, 39, 86, 20);
        add(txtPlayer1Name);
        txtPlayer1Name.setColumns(10);

        txtPlayer2Name = new JTextField();
        txtPlayer2Name.setBounds(66, 64, 86, 20);
        add(txtPlayer2Name);
        txtPlayer2Name.setColumns(10);

        txtPlayer3Name = new JTextField();
        txtPlayer3Name.setVisible(false);
        txtPlayer3Name.setBounds(66, 89, 86, 20);
        add(txtPlayer3Name);
        txtPlayer3Name.setColumns(10);

        txtPlayer4Name = new JTextField();
        txtPlayer4Name.setVisible(false);
        txtPlayer4Name.setBounds(66, 114, 86, 20);
        add(txtPlayer4Name);
        txtPlayer4Name.setColumns(10);

        txtPlayer1Age = new JTextField();
        txtPlayer1Age.setBounds(162, 39, 24, 20);
        add(txtPlayer1Age);
        txtPlayer1Age.setColumns(10);

        txtPlayer2Age = new JTextField();
        txtPlayer2Age.setBounds(162, 64, 24, 20);
        add(txtPlayer2Age);
        txtPlayer2Age.setColumns(10);

        txtPlayer3Age = new JTextField();
        txtPlayer3Age.setVisible(false);
        txtPlayer3Age.setBounds(162, 89, 24, 20);
        add(txtPlayer3Age);
        txtPlayer3Age.setColumns(10);

        txtPlayer4Age = new JTextField();
        txtPlayer4Age.setVisible(false);
        txtPlayer4Age.setBounds(162, 114, 24, 20);
        add(txtPlayer4Age);
        txtPlayer4Age.setColumns(10);

        btnAddPlayer3 = new JButton("Add Player");
        btnAddPlayer3.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent arg0) {
                btnAddPlayer3.setVisible(false);
                btnAddPlayer4.setEnabled(true);
                lblPlayer3.setVisible(true);
                txtPlayer3Name.setVisible(true);
                txtPlayer3Age.setVisible(true);
                btnRemovePlayer3.setVisible(true);
            }
        });
        btnAddPlayer3.setBounds(10, 88, 89, 23);
        add(btnAddPlayer3);

        btnAddPlayer4 = new JButton("Add Player");
        btnAddPlayer4.setEnabled(false);
        btnAddPlayer4.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                btnAddPlayer4.setVisible(false);
                lblPlayer4.setVisible(true);
                txtPlayer4Name.setVisible(true);
                txtPlayer4Age.setVisible(true);
                btnRemovePlayer4.setVisible(true);
            }
        });
        btnAddPlayer4.setBounds(10, 113, 89, 23);
        add(btnAddPlayer4);

        btnRemovePlayer3 = new JButton("Remove");
        btnRemovePlayer3.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            	if(btnRemovePlayer3.isEnabled()){
	                btnAddPlayer3.setVisible(true);
	                lblPlayer3.setVisible(false);
	                txtPlayer3Name.setVisible(false);
	                txtPlayer3Age.setVisible(false);
	                btnRemovePlayer3.setVisible(false);
	                txtPlayer3Name.setText("");
	                txtPlayer3Age.setText("");
	                txtPlayer3Name.setBackground(Color.WHITE);
	                txtPlayer3Age.setBackground(Color.WHITE);
            	}
            }
        });
        btnRemovePlayer3.setVisible(false);
        btnRemovePlayer3.setBounds(196, 88, 71, 23);
        add(btnRemovePlayer3);

        btnRemovePlayer4 = new JButton("Remove");
        btnRemovePlayer4.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
            	if(btnRemovePlayer4.isEnabled()){
	                btnAddPlayer4.setVisible(true);
	                lblPlayer4.setVisible(false);
	                txtPlayer4Name.setVisible(false);
	                txtPlayer4Age.setVisible(false);
	                btnRemovePlayer4.setVisible(false);
	                txtPlayer4Name.setText("");
	                txtPlayer4Age.setText("");
	                txtPlayer4Name.setBackground(Color.WHITE);
	                txtPlayer4Age.setBackground(Color.WHITE);
            	}
            }
        });
        btnRemovePlayer4.setVisible(false);
        btnRemovePlayer4.setBounds(196, 113, 71, 23);
        add(btnRemovePlayer4);

        JLabel lblName = new JLabel("Name:");
        lblName.setBounds(66, 14, 46, 14);
        add(lblName);

        JLabel lblAge = new JLabel("Age:");
        lblAge.setBounds(162, 14, 46, 14);
        add(lblAge);
    }

    /*
     * Validate and add a player to the game.
     */
    private boolean addPlayer(JTextField nameField, JTextField ageField) {
        String name = nameField.getText();
        if (!nameField.isEditable()) {
            return true;
        }
        int age;
        try {
            age = Integer.parseInt(ageField.getText());
        } catch (NumberFormatException e) {
            return false;
        }
        if (age <= 0) {
            return false;
        }
        if (name != null && name.length() > 0) {
            _controller.addPlayer(name, age);
            return true;
        }
        return false;
    }

    public boolean isFinished() {
        return _finished;
    }

    public void setFinished(boolean finish) {
        _finished = finish;
    }
}
